<html>
<head>
<title>Добавление контента в базу данных</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<?php


$link = mysql_connect('localhost', 'root', 'the1soop');
if (!$link) {
    die('Ошибка соединения: ' . mysql_error());
}
echo 'Успешно соединились';

$database = 'lectures';
if (!mysql_select_db($database)) {
    echo "<h1>Can't select database</h1>";
    // die("");
}

$fh = fopen("subjects.txt", "r");
$idx = 0;
$line = "";
$name = "";
while (! feof($fh)) {
	if ($idx % 2 == 1) {
		$line = fgets($fh, 4096);
		print $name ."&nbsp;". $line . "<br>";
		$result = mysql_query("insert into subject (name, description) values ('$name', '$line')");
		if (!$result) {
		      echo "<br/>Error during insert (subject)";
		}
	} else {
		$name = fgets($fh, 4096);
	}
        $idx = $idx + 1;
	print $idx . "&nbsp". "</br>";
}

fclose($fh);


$result = mysql_query("insert into user (login, password, first_name, second_name, email, profile_picture_url) 
                                 values ('katriyna', 'asd', 'Katerian', 'Zaikina', 'katriyna@gmail.com', 'katriyna.jpg')");
if (!$result) {
      echo "<br/>Error during insert (user)";
}



$fh = fopen("reading_places.txt", "r");
$idx = 0;
$name = "";
$description = "";
$logo_url = "";
while (! feof($fh)) {
	if ($idx % 3 == 0) {
		$name = fgets($fh, 4096);
	} else if ($idx % 3 == 1) {
		$description = fgets($fh, 4096);
	} else {
		$logo_url = fgets($fh, 4096);
		print $name ."&nbsp;". $description . "&nbsp" . $logo_url . "<br>";
		$result = mysql_query("insert into reading_place (name, description, logo_url) values ('$name', '$description', '$logo_url')");
		if (!$result) {
		      echo "<br/>Error during insert (reading_place)";
		}
	}
        $idx = $idx + 1;
	print $idx . "&nbsp". "</br>";
}
fclose($fh);

$fh = fopen("courses.txt", "r");
$idx = 0;
$title = "";
$description = "";
$author = "";
$reading_place_id = 0;
while (! feof($fh)) {
	if ($idx % 4 == 0) {
		$title = fgets($fh, 4096);
	} else if ($idx % 4 == 1) {
		$description = fgets($fh, 4096);
        } else if ($idx % 4 == 2) {
		$author = fgets($fh, 4096);
	} else {
		$reading_place_id = fgets($fh, 4096);
		print $title ."&nbsp;". $description . "&nbsp" . $author . "<br>";
		$result = mysql_query("insert into course (title, description, author, reading_place_id) 
                                                   values ('$title', '$description', '$author', '$reading_place_id')");
		if (!$result) {
		      echo "<br/>Error during insert (course)";
		}
	}
        $idx = $idx + 1;
	print $idx . "&nbsp". "</br>";
}
fclose($fh);


$fh = fopen("lectures.txt", "r");
$idx = 0;
$title = "";
$description = "";
$course_id = 0;
while (! feof($fh)) {
	if ($idx % 3 == 0) {
		$title = fgets($fh, 4096);
	} else if ($idx % 3 == 1) {
		$description = fgets($fh, 4096);
	} else {
		$course_id = fgets($fh, 4096) + 0;
		print $title ."&nbsp;". $description . "&nbsp" . $course_id . "<br>";
		$result = mysql_query("insert into lecture (title, description, course_id) values ('$title', '$description', '$course_id')");
		if (!$result) {
		      echo "<br/>Error during insert (lecture)";
		}
	}
        $idx = $idx + 1;
	print $idx . "&nbsp". "</br>";
}

fclose($fh);


$result = mysql_query("insert into attachment_type (name) values ('img')");
if (!$result) {
      echo "<br/>Error during insert (attachment_type) img";
}
$result = mysql_query("insert into attachment_type (name) values ('pdf')");
if (!$result) {
      echo "<br/>Error during insert (attachment_type) pdf";
}


$fh = fopen("attachments.txt", "r");
$idx = 0;
$name = "";
$description = "";
$url = "";
$lecture_id = 0;
$type_id = 1;
while (! feof($fh)) {
	if ($idx % 5 == 0) {
		$name = fgets($fh, 4096);
	} else if ($idx % 5 == 1) {
		$description = fgets($fh, 4096);
        } else if ($idx % 5 == 2) {
		$url = fgets($fh, 4096);
        } else if ($idx % 5 == 3) {
		$lecture_id = fgets($fh, 4096);
	} else {
		$type_id = fgets($fh, 4096);
		print $title ."&nbsp;". $description . "&nbsp" . $author . "<br>";
		$result = mysql_query("insert into attachment (name, description, url, lecture_id, attachment_type_id, user_id) 
                                                   values ('$name', '$description', '$url', '$lecture_id', '$type_id', '1')");
		if (!$result) {
		      echo "<br/>Error during insert (attachment)";
		}
	}
        $idx = $idx + 1;
	print $idx . "&nbsp". "</br>";
}

fclose($fh);



mysql_close($link);
?>
</body>
</html>
